package mrms.api.web.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class MessageSender {
    private static final ObjectMapper mapper = new ObjectMapper();
    public static String send(String phonenumber) throws JsonProcessingException {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI9lnAwodLEIwc", "qBxUUjv4kcbf5pyyT9zdrqpknEabLt");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phonenumber);
        request.putQueryParameter("SignName", "迅慧达");
        request.putQueryParameter("TemplateCode", "SMS_160571461");
        String code= String.valueOf((int)((Math.random()*9+1)*1000));
        Map<String, Object> map = new HashMap<>(1);
        map.put("code", code);
//        request.putQueryParameter("TemplateParam", "{\"code\":\"1111\"}");
       request.putQueryParameter("TemplateParam", mapper.writeValueAsString(map));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return code;
    }

    public static void sendtest() {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIfN2XSiKULXeB", "SfBI4vhAaUUPch0KK7cckkgFFnSwbA");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", "18157123494");
        request.putQueryParameter("SignName", "智慧达");
        request.putQueryParameter("TemplateCode", "SMS_160571427");


        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
